<template>
	<view class="mainbgc">
		<!-- 选择类别 -->
		<u-picker @close="lbshow = false" :show="lbshow" :columns="lbcolumns" @confirm="lbconfirm" @cancel="lbcancel"
			keyName="name"></u-picker>
		<header-top :messageInfo="{typeFlag:true,message:titletext,liner:false}">
			<view class="rightmsg" slot="rightmsg" v-if="type==2" @click="shancbj">
				<view class="rightmsg_content flexleft">
					<view class="shanchuer"></view>
				</view>
			</view>
		</header-top>
		<view class="mainpadding_top ffffff">
			<view class="inputbox"  @click="lbshow = true">
				<view class="flexleft flex1">
					<view class="strongtext margin_right">费用类别</view>
					<view class="bitian">*</view>
				</view>
				<view class="itembox flexbetween">
					<input type="text" placeholder="请选择费用类别" disabled="true" v-model="classfy">
					<view class="righticon2"></view>
				</view>
			</view>
			<!-- <view class="inputbox margin_top">
				<view class="flexleft flex1">
					<view class="strongtext margin_right">合同</view>
				</view>
				<view class="itembox flexbetween">
					<input type="text" placeholder="请选择合同" disabled="true" v-model="contract">
					<view class="righticon2"></view>
				</view>
			</view> -->
			<view class="inputbox margin_top">
				<view class="flexleft flex1">
					<view class="strongtext margin_right">金额</view>
					<view class="bitian">*</view>
				</view>
				<view class="itembox">
					<input type="text" placeholder="请输入金额" v-model="all_money">
				</view>
			</view>
			<view class="inputbox margin_top">
				<view class="flexleft flex1">
					<view class="strongtext margin_right">备注</view>
					<view class="bitian">*</view>
				</view>
				<view class="itembox">
					<input type="text" placeholder="请输入备注" v-model="remark">
				</view>
			</view>
		</view>
		<view class="yesbtn" @click="submit">
			提交
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				lbshow: false,
				lbcolumns: [
					[]
				],
				lbid:"",
				classfy: "", //类别名字
				contract:'', //合同
				all_money:'', //金额
				remark:'', //备注
				titletext:"填写费用",
				purchase_code:"",//单据编号
				id:'',//编辑id
				type:1,//1添加,2编辑
			}
		},
		onLoad(options) {
			let data = JSON.parse(decodeURIComponent(options.getmsg));
			if(data.type==1){//添加
				this.titletext="填写费用"
				this.purchase_code = data.code
			}else{//编辑
				this.titletext="编辑费用"
				this.id = data.code
				this.getdetail()
			}
			this.type = data.type
			this.getlblist(); //类别
		},
		methods: {
			// 删除编辑内容接口
			shancbj(){
				httpRequest.request('appapi/manage/finance/cost_expend_info_del', 'POST', {
					cost_expend_info_id:this.id,
				}).then(res => {
					// // console.log(res);
					if (res.code == 1) {
						httpRequest.toast(res.msg)
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				})
			},
			getdetail(){
				httpRequest.request('appapi/manage/finance/cost_expend_info_detail', 'POST', {
					cost_expend_info_id: this.id,
				}).then(res => {
					this.classfy = res.data.expend_type_name
					this.lbid = res.data.expend_type_id
					this.all_money = res.data.all_money
					this.remark = res.data.remark
				})
			},
			// 获取类别接口
			getlblist() {
				console.log("获取类别接口")
				httpRequest.request('appapi/manage/index/expend_type', 'POST', {
					isTree: 0,
				}).then(res => {
					// // console.log(res);
					if (res.code == 1) {
						res.data.forEach(item => {
							this.lbcolumns[0].push({
								id: item.id,
								name: item.name
							})
						})
					}
				})
			},
			//类别确定
			lbconfirm(e) {
				this.lbshow = false;
				// if (this.purchase_code != '') {
				// 	httpRequest.toast('当前单据已提交，无法进行此类操作')
				// 	return
				// }
				this.lbid = e.value[0].id;
				this.classfy = e.value[0].name;
				// this.tjfyht()
			},
			// 类别取消弹出层
			lbcancel() {
				this.lbshow = false;
			},
			submit(){
				if(this.classfy == ''){
					httpRequest.toast('请选择费用类别!')
					return false
				}
				if(this.all_money == ''){
					httpRequest.toast('请输入金额!')
					return false
				}
				if(this.remark == ''){
					httpRequest.toast('请输入备注!')
					return false
				}
				let data = {
					purchase_code:this.purchase_code,
					expend_type_id:this.lbid,
					cost_contract_id:"",
					all_money:this.all_money,
					remark:this.remark
				}
				let url = ''
				if(data.cost_contract_id==''){
					delete data.cost_contract_id
				}
				if(this.type==1){
					url = 'appapi/manage/finance/cost_expend_info_add'
				}else if(this.type==2){
					url = 'appapi/manage/finance/cost_expend_info_edit'
					data.cost_expend_info_id = this.id
					delete data.purchase_code
				}
				httpRequest.request(url, 'POST',data ).then(res => {
					// // console.log(res);
					if (res.code == 1) {
						httpRequest.toast(res.msg)
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.shanchuer{
		width: 36rpx;
		height:36rpx ;
		background-image: url('@/static/image/system/shanchu2.png');
		background-size: 100% 100%;
	}
	.flex1 {
		flex: 3;
	}

	.inputbox {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
	}

	.itembox {
		flex: 7;
		width: 100%;
		padding-bottom: 20rpx;
		border-bottom: 1rpx solid rgba(153, 153, 153, 0.06);
	}
</style>
